#include "projectHeader.h"

int main(void)
{
    int nSystem, isIns, isStu;
    char cName[64];
    instructor *ins;
    student *stu;
    nCountIns = 0;
    nCountStu = 0;

    printf ( "Log in user: \n" );
    for ( scanf ( "%s", cName ); 1; scanf ( "%s", cName ) )
    {
        FILE *fileReadIns = fopen ( "instructorInfo.txt", "r" );
        makeInsEmpty ( &ins );
        if ( fileReadIns == NULL )
        {
            printf ( "System error: File could not found\n" );
            exitProg ();
        }
        else
            ins = readInstructors ( fileReadIns );

        isIns = isInstructor ( cName, ins );
        /* check if user is instructor */

        FILE *fileReadStu = fopen ( "studentInfo.txt", "r" );
        makeStuEmpty ( &stu );
        /* open student file */
        if ( fileReadStu == NULL )
        {
            stu = NULL;
            nCountStu = 0;
        }
        else
            stu = readStudents ( fileReadStu );

        isStu = isStudent ( cName, stu );
        /* check if user is a student */

        for ( nSystem = getMenu ( isIns, isStu ); nSystem; )
        {
            if ( isIns )
            {
                switch ( nSystem )
                {
                case 1:
                    addStudentInfo ( &stu );
                    break;
                    /* add an student */
                case 2:
                    addInstructorInfo ( &ins );
                    break;
                    /* add an instructor */
                case 3:
                    changeStudentInfo ( &stu );
                    break;
                    /* change a student's info */
                case 4:
                    changeInstructorInfo ( &ins );
                    break;
                    /* change an instructor's info */
                case 5:
                    deleteStudent ( &stu );
                    break;/* delete a student */
                case 6:
                    deleteInstructor ( &ins );
                    break;
                    /* delete an instructor */
                case 7:
                    showClass ( ins, stu );
                    printf ( "Average score is %.1lf\n", getAverageScore ( stu ) );
                    break;
                    /* get student score */
                case 8:
                    saveInfo ( ins, stu );
                    nSystem = 0;
                    printf ( "Log out successful!\n\nLog in user: " );
                    break;
                    /* Log out */
                case 9:
                    fclose ( fileReadIns );
                    fclose ( fileReadStu );
                    saveInfo ( ins, stu );
                    clearInstructor ( &ins );
                    clearStudent ( &stu );
                    exitProg ();
                    break;
                    /* Exit prog */
                }

                if ( nSystem )
                    nSystem = getMenu ( isIns, isStu );
                /* show menu again */
            }
            /* instructor menu */

            else
            {
                switch ( nSystem )
                {
                case 1:
                    getStudentScore ( cName, stu );
                    break;
                case 2:
                    printf ( "Average score is %.1lf\n", getAverageScore ( stu ) );
                    break;
                case 3:
                    printf ( "Log out successful!\n\nLog in user: " );
                    nSystem = 0;
                    break;
                case 4:
                    clearInstructor ( &ins );
                    clearStudent ( &stu );
                    exitProg ();
                    break;
                }

                if ( nSystem )
                    nSystem = getMenu ( isIns, isStu );
                /* show menu again */
            }
            /* student menu */
        }

        fclose ( fileReadIns );
        fclose ( fileReadStu );
    }

    exitProg ();
    return 0;
}

